package com.ebelter.ty.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.ty.AppConfig;
import com.ebelter.ty.R;
import com.ebelter.ty.model.http.bean.HostoryListData;
import com.ebelter.ty.utils.ScaleBaseUtils;

import java.util.ArrayList;
import java.util.List;


public class HostoryListAdapter extends BaseAdapter {

    private Context mContext;

    private String unitKg = StringUtils.getResStr(R.string.kg);
    private String unitLb = StringUtils.getResStr(R.string.lb);

    public HostoryListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public List<HostoryListData.ResultDataBean.DataBean> getData() {
        return data;
    }

    private List<HostoryListData.ResultDataBean.DataBean> data;

    public void setData(List<HostoryListData.ResultDataBean.DataBean> data) {

        this.data=data;
        notifyDataSetChanged();
    }

    public void addData(List<HostoryListData.ResultDataBean.DataBean> data2) {
        if (data==null){
           data=new ArrayList<>();
        }
       data.addAll(data2);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Vh vh;
        if (convertView==null){
            convertView=View.inflate(mContext, R.layout.hostory_item,null);
            vh=new Vh();
            vh.topView=convertView.findViewById(R.id.hostory_item_top_v);
            vh.hostory_item_line_v=convertView.findViewById(R.id.hostory_item_line_v);
            vh.linearLayout=convertView.findViewById(R.id.hostory_item_ll);
            vh.hostory_item_time_tv=convertView.findViewById(R.id.hostory_item_time_tv);
            vh.hostory_item_data_tv=convertView.findViewById(R.id.hostory_item_data_tv);
            vh.hostory_item_weight2_tv=convertView.findViewById(R.id.hostory_item_weight2_tv);
            vh.hostory_item_bodyfat_per2_tv=convertView.findViewById(R.id.hostory_item_bodyfat_per2_tv);
            convertView.setTag(vh);
        }else {
            vh= (Vh) convertView.getTag();
        }

        String unit= AppConfig.getInstance().isKgModel()?unitKg:unitLb;
        String time= ScaleBaseUtils.timeFatmat(data.get(position).getTestDate(),2);
        if (!TextUtils.isEmpty(time)){
            if (time.length()>6){
                vh.hostory_item_time_tv.setTextSize(15);
            }else {
                vh.hostory_item_time_tv.setTextSize(20);
            }
        }
        vh.hostory_item_time_tv.setText(time);
        vh.hostory_item_data_tv.setText(ScaleBaseUtils.timeFatmat(data.get(position).getTestDate(),0));

        float w=ScaleBaseUtils.adapterDataFloat((float) data.get(position).getWeight());
        w= NumUtils.numBaoLiuWei1(w);
        vh.hostory_item_weight2_tv.setText(numFat(w,unit));

        float fat=NumUtils.numBaoLiuWei1((float) data.get(position).getAdiposeRate());
        vh.hostory_item_bodyfat_per2_tv.setText(numFat2(fat,"%"));

        updateView(vh.linearLayout,vh.topView,vh.hostory_item_line_v,position);

        return convertView;
    }

    private String numFat(float value,String unit){
        if (value<=0){
            return NO_RESULT;
        }
        if (TextUtils.equals(unit,unitLb)){
            return ScaleBaseUtils.unitDisplay2(value+"",unitLb);
        }

        return value+unit;
    }

    public static final String NO_RESULT = "--";
    private String numFat2(float value,String unit){
        if (value<=0){
            return NO_RESULT;
        }
        String local = AppUtils.getLocalStr();
        if (TextUtils.equals(local,AppUtils.iw)){
            return unit+value;
        }else {
            return value+unit;
        }
    }

    //更新顶部的显示已否
    private void updateView(LinearLayout linearLayout,View topView, View lineView,int position) {

        if (data==null||data.get(position)==null){
            return;
        }
        String lastTime = null;
        String currentTime=null;
        String nextTime = null;
        try {
            currentTime=data.get(position).getTestDate().substring(0,10);
            HostoryListData.ResultDataBean.DataBean last=data.get(position-1);
            lastTime=last.getTestDate().substring(0,10);
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            HostoryListData.ResultDataBean.DataBean next=data.get(position+1);
            nextTime=next.getTestDate().substring(0,10);
        }catch (Exception e){
            e.printStackTrace();
        }


        if (currentTime!=null&&TextUtils.equals(lastTime,currentTime)){
            //当前时间与上一条时间是同一天
            ViewUtils.goneView(topView);
            linearLayout.setBackgroundResource(R.drawable.sp_white_lb6_rb6);
        }else {
            ViewUtils.displayView(topView);
            linearLayout.setBackgroundResource(R.drawable.sp_white_all6);
        }

        if (currentTime!=null&&TextUtils.equals(currentTime,nextTime)){
            //当前时间与下一条时间是同一天
            ViewUtils.displayView(lineView);
            linearLayout.setBackgroundResource(R.drawable.sp_white_lt6_rt6);
        }else {
            ViewUtils.goneView(lineView);
            linearLayout.setBackgroundResource(R.drawable.sp_white_all6);
        }

        if (currentTime!=null&&TextUtils.equals(currentTime,nextTime)&&TextUtils.equals(lastTime,currentTime)){
            //当前时间与上，下一条时间是同一天
            linearLayout.setBackgroundResource(R.drawable.sp_white);
        }
        if (currentTime!=null&&TextUtils.equals(lastTime,currentTime)&&!TextUtils.equals(currentTime,nextTime)){
            //当前时间与上同一天，与下一条时间不是同一天
            linearLayout.setBackgroundResource(R.drawable.sp_white_lb6_rb6);
        }








    }



    private class Vh{
        View topView;
        View hostory_item_line_v;
        LinearLayout linearLayout;
        TextView hostory_item_time_tv;
        TextView hostory_item_data_tv;
        TextView hostory_item_weight2_tv;
        TextView hostory_item_bodyfat_per2_tv;
    }
}
